package com.wisn.appcheckmethod.plugin.hook;

import android.app.ActivityManager;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Looper;

import java.net.Inet4Address;
import java.net.Inet6Address;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

/**
 * xposed关键类，实现IXposedHookLoadPackage，在handleLoadPackage中，
 * 通过XposedHelpers.findAndHookMethod来hook具体的方法。
 */
public class MethodHook implements IXposedHookLoadPackage {
    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) {
        if (loadPackageParam == null) return;
        initPrivacyHook(loadPackageParam);
    }

    private void initPrivacyHook(XC_LoadPackage.LoadPackageParam lpparam) {
        XposedHelpers.findAndHookMethod(
                android.telephony.TelephonyManager.class.getName(),
                lpparam.classLoader,
                "getImei",
                new MethodSettle(lpparam.processName, lpparam.packageName,"设备号getImei"));

        XposedHelpers.findAndHookMethod(
                android.telephony.TelephonyManager.class.getName(),
                lpparam.classLoader,
                "getImei",
                int.class,
                new MethodSettle(lpparam.processName, lpparam.packageName,"设备号getImei"));

        XposedHelpers.findAndHookMethod(
                android.telephony.TelephonyManager.class.getName(),
                lpparam.classLoader,
                "getDeviceId",
                new MethodSettle(lpparam.processName, lpparam.packageName,"设备号getDeviceId"));

        XposedHelpers.findAndHookMethod(
                android.telephony.TelephonyManager.class.getName(),
                lpparam.classLoader,
                "getDeviceId",
                int.class,
                new MethodSettle(lpparam.processName, lpparam.packageName,"设备号getDeviceId"));

        XposedHelpers.findAndHookMethod(
                android.telephony.TelephonyManager.class.getName(),
                lpparam.classLoader,
                "getSubscriberId",
                int.class,
                new MethodSettle(lpparam.processName, lpparam.packageName,"设备号getSubscriberId"));

        XposedHelpers.findAndHookMethod(
                android.net.wifi.WifiInfo.class.getName(),
                lpparam.classLoader,
                "getMacAddress",
                new MethodSettle(lpparam.processName, lpparam.packageName,"设备getMacAddress"));

        XposedHelpers.findAndHookMethod(
                Inet6Address.class.getName(),
                lpparam.classLoader,
                "getHostAddress",
                new MethodSettle(lpparam.processName, lpparam.packageName,"设备Inet6Address.getHostAddress"));

        XposedHelpers.findAndHookMethod(
                Inet4Address.class.getName(),
                lpparam.classLoader,
                "getHostAddress",
                new MethodSettle(lpparam.processName, lpparam.packageName,"设备Inet4Address.getHostAddress"));

        XposedHelpers.findAndHookMethod(
                java.net.NetworkInterface.class.getName(),
                lpparam.classLoader,
                "getHardwareAddress",
                new MethodSettle(lpparam.processName, lpparam.packageName,"设备信息getHardwareAddress"));

        XposedHelpers.findAndHookMethod(
                android.provider.Settings.Secure.class.getName(),
                lpparam.classLoader,
                "getString",
                android.content.ContentResolver.class,
                String.class,
                new MethodSettle(lpparam.processName, lpparam.packageName,"设备信息getString"));


//        XposedHelpers.findAndHookMethod(
//                PackageManager.class.getName(),
//                lpparam.classLoader,
//                "getInstalledPackages",
//                int.class,
//                new MethodSettle(lpparam.processName, lpparam.packageName, "获取了软件安装列表"));
        XposedHelpers.findAndHookMethod(
                "android.app.ApplicationPackageManager",//getInstalledPackages接口本身是一个抽象方法，在ApplicationPackageManager类中进行了该抽象方法的具体实现，所以我们Hook的目标类是ApplicationPackageManager，目标方法是getInstalledPackages(Int)。
                lpparam.classLoader,
                "getInstalledPackages",
                int.class,
                new MethodSettle(lpparam.processName, lpparam.packageName, "获取了软件安装列表"));

        XposedHelpers.findAndHookMethod(
                "android.app.ApplicationPackageManager",
                lpparam.classLoader,
                "getPackageInfo",
                String.class, int.class,
                new MethodSettle(lpparam.processName, lpparam.packageName,"获取了应用信息getPackageInfo"));

        XposedHelpers.findAndHookMethod(
                ActivityManager.class.getName(),
                lpparam.classLoader,
                "getRunningAppProcesses",
                new MethodSettle(lpparam.processName, lpparam.packageName,"读取当前运行应用进程"));

        XposedHelpers.findAndHookMethod(
                Build.class.getName(),
                lpparam.classLoader,
                "getSerial",
                new MethodSettle(lpparam.processName, lpparam.packageName,"调用getSerial获取设备序列号"));

        XposedHelpers.findAndHookMethod(
                android.location.LocationManager.class.getName(),
                lpparam.classLoader,
                "getLastKnownLocation",
                String.class,
                new MethodSettle(lpparam.processName, lpparam.packageName,"getLastKnownLocation获取了GPS地址"));


        XposedHelpers.findAndHookMethod(
                "android.os.SystemProperties",
                lpparam.classLoader,
                "get",
                String.class,
                new MethodSettle(lpparam.processName, lpparam.packageName,"调用SystemProperties获取"));

        XposedHelpers.findAndHookMethod(
                LocationManager.class.getName(),
                lpparam.classLoader,
                "requestLocationUpdates",
                String.class,
                long.class,
                float.class,
                LocationListener.class,
                Looper.class,
                new MethodSettle(lpparam.processName, lpparam.packageName,"调用requestLocationUpdates"));

        XposedHelpers.findAndHookMethod(
                LocationManager.class.getName(),
                lpparam.classLoader,
                "requestLocationUpdates",
                String.class,
                long.class,//注意，如果是基础类型，不要使用其对应的包装类，否则会找不到这个方法
                float.class,
                LocationListener.class,
                new MethodSettle(lpparam.processName, lpparam.packageName,"调用requestLocationUpdates"));
    }
}

